/**
 * @file 用户数据
 * @author its-wild(https://gitee.com/its-wild)
 * @date 2022/07/27
 */
import { defineStore } from 'pinia'
import getMenu from '@/sso/menu'
import { useWhiteListStore } from '@/store/whiteList'

export const useUserStore = defineStore('useUserStore', {
  state: (): IUserStore => {
    return {
      user: null
    }
  },
  actions: {
    setUser(data: IUser) {
      this.user = data || null
      const whiteListStore = useWhiteListStore()
      // 菜单
      const menu = getMenu()
      whiteListStore.setMenu(menu)
      // 菜单组成的白名单
      const whiteList: Array<string> = []
      menu.forEach(x => {
        if (!x.children) {
          whiteList.push(x.name)
        } else {
          x.children.forEach(y => {
            if (!y.children) {
              whiteList.push(y.name)
            } else {
              y.children.forEach(z => {
                if (!z.children) {
                  whiteList.push(z.name)
                }
              })
            }
          })
        }
      })
      whiteListStore.setWhiteList(whiteList)
    }
  }
})
